/*
*************************************************************************
*西瑞克斯通信技术股份有限公司研发管理系统
*版 本: 1.0   
*日 期：02/05/2012    
*作 者： 王浩 QQ:873987060   
*************************************************************************   
*/  
package com.application.yfproject.struts.action;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.application.yfproject.commons.Constants;
import com.application.yfproject.entity.YfProject;
import com.application.yfproject.service.YfProjectService;
import com.application.yfproject.struts.form.YfProjectForm;
import com.application.yfprojectnode.entity.YfProjectNode;
import com.application.yfprojectnode.service.YfProjectNodeService;
import com.application.yfprojectnode.struts.form.YfProjectNodeForm;
import com.manager.commons.CommonUtil;
import com.manager.commons.RandomKey;
import com.manager.commons.page.PageBean;
 /**
 * 
 * 
 * 
 * Description:YfProjectAction<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 <br />
 * 
 * History: <br />
 * 
 * 02/05/2012 王浩 QQ:873987060   
 * 
 */
public class YfProjectAction extends DispatchAction
{
       
       /**
	 * Service层实例
	 */
	private YfProjectService yfProjectService;
	
	private YfProjectNodeService yfProjectNodeService;

	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 实体对象
	 */
        private YfProject yfProject;


        /**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getYfProjectPartialList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchYfProject" + "开始");
		YfProjectForm yfProject = (YfProjectForm)form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List yfProjectList = null;
		resultSize = yfProjectService.getAllCountYfProject(yfProject);
		pageBean = PageBean.getPageBean("yfProjectTab", resultSize,request);
		yfProjectList = yfProjectService.searchYfProject(yfProject,pageBean);
		request.setAttribute("yfProjectList", yfProjectList);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		log.debug("searchYfProject" + "结束");
		return mapping.findForward(Constants.SUCCESS);
	}
	
	
    /**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getYfProjectViewList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("getYfProjectViewList" + "开始");
		YfProjectForm yfProject = (YfProjectForm)form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List yfProjectList = null;
		resultSize = yfProjectService.getAllCountYfProjectXMST(yfProject);
		pageBean = PageBean.getPageBean("yfProjectTab", resultSize,request);
		yfProjectList = yfProjectService.searchYfProjectXMST(yfProject,pageBean);
		request.setAttribute("yfProjectList", yfProjectList);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		log.debug("getYfProjectViewList" + "结束");
		return mapping.findForward(Constants.SUCCESS);
	}

	
    /**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward nodeStatusList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchYfProject" + "开始");
		YfProjectForm yfProject = (YfProjectForm)form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List yfProjectList = null;
		resultSize = yfProjectService.getAllCountYfProjectXMST(yfProject);
		pageBean = PageBean.getPageBean("yfProjectTab", resultSize,request);
		yfProjectList = yfProjectService.searchYfProjectXMST(yfProject,pageBean);
		request.setAttribute("yfProjectList", yfProjectList);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		log.debug("searchYfProject" + "结束");
		return mapping.findForward("node_list");
	}
	
	
        /**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddYfProject(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toAddYfProjects");
		YfProjectForm yfProjectForm = (YfProjectForm) form;
		yfProjectForm.setInitiateYear(Long.valueOf(CommonUtil.getCurrentYear()));
		yfProjectForm.setInitiatePerson(CommonUtil.getCurrentUser(request).getRname());
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addYfProject(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addYfProjects");
		YfProject yfProject = this.getYfProject(form);
        yfProject.setId(RandomKey.generateID_Long_19());
        String pix = CommonUtil.getProjectNoPix(yfProject.getProjectOrg());
        String projectNo = yfProjectService.getMaxProjectNo(pix);
        yfProject.setProjectNo(projectNo);
        yfProject.setOperateid(CommonUtil.getCurrentUser(request).getId());
		yfProject.setCreateDate(new Date());
		
		this.yfProjectService.insertObject(yfProject);
		
		YfProjectNode node = new YfProjectNode();
		node.setId(RandomKey.generateID_Long_19());
		node.setProjectId(yfProject.getId());
		yfProjectNodeService.insertObject(node);

		return mapping.findForward(Constants.SUCCESS);
	}
        
	/**
	 * 删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward deleteYfProject(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("deleteYfProjects");
		YfProjectForm yfProjectForm = (YfProjectForm) form;
		this.yfProjectService.deleteYfProjects(yfProjectForm);
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toUpdateYfProject(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toUpdateYfProjects");
		YfProjectForm yfProjectForm = (YfProjectForm) form;
		yfProject = this.yfProjectService.searchYfProject(yfProjectForm, null).get(0);
		request.setAttribute(Constants.YFPROJECT, yfProject);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}

        
	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateYfProject(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("updateYfProjects");
		YfProject yfProject = this.getYfProject(form);
		yfProject.setOperateid(CommonUtil.getCurrentUser(request).getId());
		yfProject.setCreateDate(new Date());
		this.yfProjectService.updateObject(yfProject);

		return mapping.findForward(Constants.SUCCESS);
	}
        
	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewYfProject(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("viewYfProject");

		YfProjectForm yfProjectForm = (YfProjectForm) form;
		yfProject = this.yfProjectService.searchYfProject(yfProjectForm, null).get(0);
		request.setAttribute(Constants.YFPROJECT, yfProject);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewNodeStatus(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("viewYfProject");

		YfProjectForm yfProjectForm = (YfProjectForm) form;
		yfProject = this.yfProjectService.searchYfProject(yfProjectForm, null).get(0);
		request.setAttribute(Constants.YFPROJECT, yfProject);
		
		YfProjectNodeForm node = new YfProjectNodeForm();
		node.setProjectId(yfProject.getId());
		YfProjectNode yfProjectNode = yfProjectNodeService.searchYfProjectNode(node, null).get(0);
		request.setAttribute("yfProjectNode", yfProjectNode);
//		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward("view_node");
	}
	
	
	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toModifyNodeStatus(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toModifyNodeStatus");
		YfProjectForm yfProjectForm = (YfProjectForm) form;
//		yfProject = this.yfProjectService.searchYfProject(yfProjectForm, null).get(0);
//		request.setAttribute(Constants.YFPROJECT, yfProject);
		return mapping.findForward("modify_node");
	}

	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateNodeStatus(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("updateNodeStatus");
		YfProjectForm yfProjectForm = (YfProjectForm) form;
        String id = request.getParameter("id");
        Long nodeStatus = Long.valueOf(request.getParameter("nodeStatus"));
        Long nodeName = Long.valueOf(request.getParameter("nodeName"));

        YfProjectNodeForm yfProjectNodeForm = new YfProjectNodeForm();
        yfProjectNodeForm.setProjectId(id);
        YfProjectNode oldNode = yfProjectNodeService.searchYfProjectNode(yfProjectNodeForm, null).get(0);
        
        int val = nodeName.intValue();
        
        switch(val){
           case 1:
        	   oldNode.setNode1(nodeStatus);
        	   break;
           case 2:
        	   oldNode.setNode2(nodeStatus);
        	   break;
           case 3:
        	   oldNode.setNode3(nodeStatus);
        	   break;
           case 4:
        	   oldNode.setNode4(nodeStatus);
        	   break;
           case 5:
        	   oldNode.setNode5(nodeStatus);
        	   break;
           case 6:
        	   oldNode.setNode6(nodeStatus);
        	   break;
           case 7:
        	   oldNode.setNode7(nodeStatus);
        	   break;
           case 8:
        	   oldNode.setNode8(nodeStatus);
        	   break;
           case 9:
        	   oldNode.setNode9(nodeStatus);
        	   break;
           case 10:
        	   oldNode.setNode10(nodeStatus);
        	   break;
        }
        
        yfProjectNodeService.updateObject(oldNode);
		return viewNodeStatus(mapping,form,request,response);
	}
        
	
	// get--set
	/**
	 * @return the YfProjectService
	 */
	public YfProjectService getYfProjectService() {
		return this.yfProjectService;
	}

	/**
	 * @param YfProjectService
	 *  the YfProjectService to set
	 */
	public void setYfProjectService(YfProjectService yfProjectService) {
		this.yfProjectService = yfProjectService;
	}

	/**
	 * 获取YfProject实体
	 * @param ActionForm
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @return the YfProject
	 */
	public YfProject getYfProject(ActionForm form) throws IllegalAccessException, InvocationTargetException {
		YfProjectForm yfProjectForm = (YfProjectForm) form;
		yfProject = new YfProject();
		BeanUtils.copyProperties(yfProject, yfProjectForm);
		return yfProject;
	}
    
        public void setYfProject(YfProject yfProject) {
		this.yfProject = yfProject;
	}
        
        
    public ActionForward popuSelectProjectList(ActionMapping mapping,
    			ActionForm form, HttpServletRequest request,
    			HttpServletResponse response) throws Exception {
    		log.debug("searchYfProject" + "开始");
    		YfProjectForm yfProject = (YfProjectForm)form;
 
    		// 统计总记录数的sql语句
    		int resultSize = 0;
    		// 初始化pageBean供分页所用
    		PageBean pageBean = null;
    		// 取得当前页list数据
    		List yfProjectList = null;
    		resultSize = yfProjectService.getAllCountYfProject(yfProject);
    		pageBean = PageBean.getPageBean("yfProjectTab", resultSize,request);
    		yfProjectList = yfProjectService.searchYfProject(yfProject,pageBean);
    		request.setAttribute("yfProjectList", yfProjectList);
    		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
    		log.debug("searchYfProject" + "结束");
    		return mapping.findForward(Constants.SUCCESS);
    	}


	public YfProjectNodeService getYfProjectNodeService() {
		return yfProjectNodeService;
	}


	public void setYfProjectNodeService(YfProjectNodeService yfProjectNodeService) {
		this.yfProjectNodeService = yfProjectNodeService;
	}

}
